import { downFile } from '/@/api/sys/file';
import { useMessage } from '/@/hooks/web/useMessage';
import {router} from "/@/router";
import {defHttp} from "/@/utils/http/axios";
const { createMessage } = useMessage();
/**
 * 导出数据
 *
 */
export function handleExportExcelData(query,exportDataUrl,downLoadUrl) {
  createMessage.loading({ content: '正在导出中', duration: 300 });
  defHttp
    .request({
      url: exportDataUrl,
      params: query,
      method: 'get',
      timeout: 300000,
    })
    .then((res) => {
      var obj = eval('(' + res + ')');
      if (obj.success === true) {
        var fileName = obj.data.substring(obj.data.lastIndexOf('/') + 1, obj.data.length);
        downFile(downLoadUrl + obj.data).then((data) => {
          if (!data) {
            createMessage.warning('文件导出失败,请重新下载');
            return;
          }
          if (data.size == 0) {
            createMessage.warning('文件导出异常,请稍后重新下载');
            return;
          }
          const nav = window.navigator as any;
          if (typeof nav.msSaveBlob !== 'undefined') {
            nav.msSaveBlob(new Blob([data], { type: 'application/vnd.ms-excel' }), fileName);
          } else {
            const url = window.URL.createObjectURL(
              new Blob([data], { type: 'application/vnd.ms-excel' }),
            );
            const link = document.createElement('a');
            link.style.display = 'none';
            link.href = url;
            link.setAttribute('download', fileName);
            document.body.appendChild(link);
            link.click();
            document.body.removeChild(link); //下载完成移除元素
            window.URL.revokeObjectURL(url); //释放掉blob对象
          }
          //清除loading
          createMessage.destroy();
        });
      }
    });

}
export function timestamp() {
  const date = new Date();
  const year = date.getFullYear(); // 年
  const month = date.getMonth() + 1; // 月
  const day = date.getDate(); // 日
  const hour = date.getHours(); // 时
  const minutes = date.getMinutes(); // 分
  const seconds = date.getSeconds(); // 秒
  return (
    year +
    month.toString().padStart(2, '0') +
    day.toString().padStart(2, '0') +
    hour.toString().padStart(2, '0') +
    minutes.toString().padStart(2, '0') +
    seconds.toString().padStart(2, '0')
  );
}

export function handleExportFileList(){
  console.log("123")
  createMessage.destroy();
  //跳转到系统管理-导出文件列表
  router.replace('/system/exportFile')
}
